﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.IO;
using System.Web.Mvc;
using StockPredictor.Models;
using StockPredictor.App_Start;
using System.Collections;

namespace StockPredictor.Controllers
{
    
    public class StockValuationController : Controller
    {
        private static Hashtable StocksToBuyCache = new Hashtable();
        private static Hashtable StocksToSellCache = new Hashtable();

        //
        // GET: /YahooFinance/
        //[OutputCache(Duration = 5184000)]
        public ActionResult Index()
        {
          if (User.Identity.IsAuthenticated)
           {
               return View();
           }
           else
           {
               return RedirectToAction("Login", "Account");
           }
        }

       
        public ActionResult _StockToBuy(FormCollection fc, string searchSymbol) 
        {
            List<CurrentStock> stocks;

            if (Request.HttpMethod == "GET")
            {
                ViewBag.Title = "Stocks To Buy";

                // format user input
                AlgorithmHistoryKey key = new AlgorithmHistoryKey();
                key.userId = AccountUtilities.GetUserIdForUser(User.Identity.Name);
                try
                {
                    key.PERatio = UserPreferencesUtilities.GetUserPreferencesRecordsForUser(User.Identity.Name).FirstOrDefault().PeRatioPrcntg;
                    key.EPS = UserPreferencesUtilities.GetUserPreferencesRecordsForUser(User.Identity.Name).FirstOrDefault().EpsPrcntg;
                }
                catch (Exception e)
                {
                    key = GenerateDefaultKey(key.userId);
                }

                if (StocksToBuyCache.ContainsKey(key))
                {
                    stocks = (List<CurrentStock>)StocksToBuyCache[key];
                }
                else
                {
                    stocks = StockValuation.GetStocksToBuy(User.Identity.Name);
                    StocksToBuyCache.Add(key, stocks);
                }

                ViewBag.StockToBuy = stocks.ToList();

                ViewBag.StocksToSell = GetStocksToSell(User.Identity.Name);
            }
            // return view
            return Index();
            
        }

        private static List<CurrentStock> GetStocksToSell(string userName)
        {
            List<CurrentStock> stocks;

            if (userName != "")
            {
                // format user input  
                AlgorithmHistoryKey key = new AlgorithmHistoryKey();
                key.userId = AccountUtilities.GetUserIdForUser(userName);
                try
                {
                    key.PERatio = UserPreferencesUtilities.GetUserPreferencesRecordsForUser(userName).FirstOrDefault().PeRatioPrcntg;
                    key.EPS = UserPreferencesUtilities.GetUserPreferencesRecordsForUser(userName).FirstOrDefault().EpsPrcntg;
                }
                catch (Exception e)
                {
                    key = GenerateDefaultKey(key.userId);
                }

                if (StocksToSellCache.ContainsKey(key))
                {
                    stocks = (List<CurrentStock>)StocksToSellCache[key];
                }
                else
                {
                    stocks = StockValuation.GetStocksToSell(userName);
                    StocksToSellCache.Add(key, stocks);
                }

                return stocks.ToList();
            }
            else
            {
                // no username was specified so we should return an empty list
                return new List<CurrentStock>();
            }
        }

        private static AlgorithmHistoryKey GenerateDefaultKey(int UserId)
        {
            AlgorithmHistoryKey key = new AlgorithmHistoryKey();
            key.userId = UserId;
            key.EPS = 20;
            key.PERatio = 10;

            return key;
        }
    }
}
